package com.example.yupao.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * @Author hhf
 * @Date 2024/9/11
 * @Description
 */
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {

        //1.创建RedisTemplate对象
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        //2.设置连接工厂
        template.setConnectionFactory(redisConnectionFactory);
        //3.创建JSON序列化工具,String序列化工具
        RedisSerializer<Object> jsonRedisSerializer = RedisSerializer.json();
        RedisSerializer<String> stringRedisSerializer = RedisSerializer.string();
        //4.为RedisTemplate对象设置key序列化工具
        template.setKeySerializer(stringRedisSerializer);
        template.setHashKeySerializer(stringRedisSerializer);
        //5.为RedisTemplate对象设置value序列化工具
        template.setValueSerializer(jsonRedisSerializer);
        template.setHashValueSerializer(jsonRedisSerializer);
        //6.返回RedisTemplate对象
        return template;

    }

    /**
     * 修改 session-redis的session乱码问题
     */
    //TODO 原理
//    @Bean
//    public RedisSerializer<Object> springSessionDefaultRedisSerializer() {
//        RedisSerializer<Object> serializer = RedisSerializer.json();
//        return serializer;
//    }


}
